package cn.antalk.top.normal.controller;


import cn.antalk.top.normal.entity.Comment;
import cn.antalk.top.normal.entity.CommentForShow;
import cn.antalk.top.normal.entity.Talk_User;
import cn.antalk.top.normal.service.TalkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: WackyBoy
 * @Date: 2021/12/4 19:01
 */

@RestController
@RequestMapping("/normal")
public class TalkController {

    @Autowired
    public TalkService talkService;

    @PostMapping("/pushTalk")
    public ResponseEntity pushTalk(@RequestParam("uid") String uid, @RequestParam("content") String content) {
        int user_id = Integer.parseInt(uid);
        boolean flag = this.talkService.pushTalk(user_id, content);
        if(flag) {
            return ResponseEntity.ok().build();
        } else return ResponseEntity.status(400).build();

    }

    @GetMapping("/getTalkList_squre")
    public ResponseEntity<List<Talk_User>> getTalkList_squre(@RequestParam("page") int page, @RequestParam("count") int count) {
        List<Talk_User> talk_userList = this.talkService.getTalkByPageAndCount(page, count);
        return ResponseEntity.ok(talk_userList);
    }

    @PostMapping("/like")
    public ResponseEntity<Integer> changeLikeState(@RequestParam("uid") int uid, @RequestParam("talk_id") int talk_id) {
        int like_total = this.talkService.changeLikeState(uid, talk_id);
        return ResponseEntity.ok(like_total);
    }

    @GetMapping("/getTalkComment")
    public ResponseEntity<List<CommentForShow>> getTalkComment(@RequestParam("talkId") int talkId, HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        boolean hasCookie = false;
        if (cookies != null) hasCookie = true;
        List<CommentForShow> commentList = this.talkService.getTalkCommentAllInfoByTalkId(talkId, hasCookie);
        return ResponseEntity.ok(commentList);
    }

    @PostMapping("/postTalkComment")
    public ResponseEntity postTalkComment(@RequestParam("content") String content, @RequestParam("talkId") int talkId, @RequestParam("talkUid") int talkUid, @RequestParam("uId") int uId) {
        boolean flag = this.talkService.postTalkComment(content, talkId, talkUid, uId);
        return flag ? ResponseEntity.ok().build() : ResponseEntity.status(HttpStatus.REQUEST_TIMEOUT).build();
    }

    // user like comment or cancel likeStatus
    @PostMapping("/likeComment")
    public ResponseEntity likeComment(@RequestParam("comment_id") int comment_id, @RequestParam("u_id") int u_id, @RequestParam("likeStatus") boolean likeStatus) {
        Comment comment = this.talkService.changeCommentLikeState(comment_id, u_id, likeStatus);
        return ResponseEntity.ok(comment);
    }



}
